﻿using System;
using Newtonsoft.Json.Linq;

namespace Wala.Connection.Objects.Timeline
{
    public class Comment
    {
        #region //--------- Private Fields --------------//

        private long _comment_id;
        private int _owner_id;
        private string _owner_display_name;
        private string _text;
        private int _target_id;
        private int _target_type;
        private long _time_stamp;
        private double _location_x;
        private double _location_y;
        private ulong _owner_avatar_id;
        private long _owner_cover_id;

        #endregion

        #region //--------- CLR Properties --------------//
        public long Comment_ID
        {
            get { return _comment_id; }
            set { _comment_id = value; }
        }

        public int Owner_ID
        {
            get { return _owner_id; }
            set { _owner_id = value; }
        }

        public string Owner_Display_Name
        {
            get { return _owner_display_name; }
            set { _owner_display_name = value; }
        }

        public string Text
        {
            get { return _text; }
            set { _text = value; }
        }

        public int Target_Id
        {
            get { return _target_id; }
            set { _target_id = value; }
        }

        public int Target_Type
        {
            get { return _target_type; }
            set { _target_type = value; }
        }

        public long Time_Stamp
        {
            get { return _time_stamp; }
            set { _time_stamp = value; }
        }

        public double Location_X
        {
            get { return _location_x; }
            set { _location_x = value; }
        }

        public double Location_Y
        {
            get { return _location_y; }
            set { _location_y = value; }
        }

        public ulong Owner_Avatar_Id
        {
            get { return _owner_avatar_id; }
            set { _owner_avatar_id = value; }
        }

        public long Owner_Cover_Id
        {
            get { return _owner_cover_id; }
            set { _owner_cover_id = value; }
        }
        #endregion

        #region //--------- Public Constructors ---------//

        public Comment()
        {

        }

        public Comment(long commentID, int ownerID, string ownerDisplayName, string text, int targetId,
            int targetType, long timeStamp, double locationX, double locationY, ulong ownerAvatarId, long ownerCoverId)
        {
            _owner_cover_id = ownerCoverId;
            _owner_avatar_id = ownerAvatarId;
            _location_x = locationX;
            _location_y = locationY;
            _time_stamp = timeStamp;
            _target_type = targetType;
            _target_id = targetId;
            _text = text;
            _owner_display_name = ownerDisplayName;
            _owner_id = ownerID;
            _comment_id = commentID;
        }

        #endregion

        #region //--------- Public Methods --------------//
        public int CompareTo(Comment another)
        {
            if (another.Comment_ID > _comment_id) return -1;
            if (another.Comment_ID < _comment_id) return 1;
            return 0;
        }

        public static Comment FromJson(string json)
        {
            try
            {
                JObject jObject = JObject.Parse(json);
                long comment_id = (long)jObject["comment_id"];
                int owner_id = (int)jObject["owner_id"];
                string owner_display_name = (string)jObject["owner_display_name"];
                string text = (string)jObject["text"];
                int target_id = (int)jObject["target_id"];
                int target_type = (int)jObject["target_type"];
                long time_stamp = (long)jObject["time_stamp"];
                double location_x = (double)jObject["location_x"];
                double location_y = (double)jObject["location_y"];
                ulong owner_avatar_id = (ulong)jObject["owner_avatar_id"];
                long owner_cover_id = (long)jObject["owner_cover_id"];

                Comment comment = new Comment(comment_id, owner_id, owner_display_name,
                                            text, target_id, target_type, time_stamp,
                                            location_x, location_y, owner_avatar_id, owner_cover_id);
                return comment;
            }
            catch (Exception ex)
            {
                return new Comment();
            }

        }
        #endregion
    }
}
